package com.pswv2.backend.popularize.imp;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pswv2.backend.popularize.service.PopularizeService;
import com.pswv2.bub.bsc.dao.po.Popularize;
import com.pswv2.bub.bsc.dao.po.PopularizeImg;
import com.pswv2.util.ExtraSpringHibernateTemplate;


@Service
@SuppressWarnings("unchecked")
public class PopularizeServiceImpl implements PopularizeService {
	private static Class<?> poc = Popularize.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	@Override
	public List<Popularize> queryByState(Integer state) {
		// TODO Auto-generated method stub
		List<Popularize> ls=extraSpringHibernateTemplate.findAllByPropEq(poc, "state", state);
		return ls;
	}
	@Override
	public List<?> queryByUserId(Integer userId) {
		// TODO Auto-generated method stub
		List<?> ls=extraSpringHibernateTemplate.getHibernateTemplate().find("SELECT p,t.name FROM Popularize p ,Territory t WHERE "
				+ " p.territoryId=t.territoryId AND p.userId = ?", userId);
		return ls;
	}
	@Override
	public Popularize queryById(Integer popularizeId) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Popularize add(Integer userId, Integer territoryId, String title, String address, String content) {
		// TODO Auto-generated method stub
		Popularize entity=new Popularize();
		entity.setContent(content);
		entity.setAddress(address);
		entity.setTerritoryId(territoryId);
		entity.setState(0);
		entity.setTime(new Date().getTime());
		entity.setTitle(title);
		entity.setUserId(userId);
		extraSpringHibernateTemplate.getHibernateTemplate().save(entity);
		return entity;
	}
	@Override
	public void delById(Integer popularizeId) {
		// TODO Auto-generated method stub
		Popularize entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "popularizeId", popularizeId);
		extraSpringHibernateTemplate.getHibernateTemplate().delete(entity);
	}
	@Override
	public void editById(Integer popularizeId,  Integer territoryId, String title, String popularizeDescribe,
			String content) {
		// TODO Auto-generated method stub
		Popularize entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "popularizeId", popularizeId);
		entity.setContent(content);
		entity.setPopularizeDescribe(popularizeDescribe);
		entity.setTerritoryId(territoryId);
		entity.setTitle(title);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
	}
	@Override
	public void editState(Integer popularizeId, Integer state) {
		// TODO Auto-generated method stub
		Popularize entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "popularizeId", popularizeId);
		entity.setState(state);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
	}
	@Override
	public List<Popularize> queryAll() {
		// TODO Auto-generated method stub
		List<Popularize> ls=(List<Popularize>) extraSpringHibernateTemplate.findAll(poc);
		return ls;
	}
	@Override
	public void addPopularizeServiceImpl(Integer popularizeId, String img) {
		// TODO Auto-generated method stub
		PopularizeImg entity=new PopularizeImg();
		entity.setImg(img);
		entity.setPopularizeId(popularizeId);
		extraSpringHibernateTemplate.getHibernateTemplate().save(entity);
	}
	@Override
	public List<PopularizeImg> queryByPopularizeId(Integer popularizeId) {
		// TODO Auto-generated method stub
		List<PopularizeImg> ls=extraSpringHibernateTemplate.findAllByPropEq(PopularizeImg.class, "popularizeId", popularizeId);
		return ls;
	}
	@Override
	public List<?> queryByTitle(String title) {
		// TODO Auto-generated method stub
		List<?> lsit=extraSpringHibernateTemplate.getHibernateTemplate()
				.find("SELECT p,t.name FROM Popularize p ,Territory t WHERE  p.territoryId=t.territoryId AND p.title LIKE ?", "%"+title+"%" );
		return lsit;
	}
	@Override
	public void editById(Integer popularizeId, String keyDescribe1, String keyDescribe2, String keyDescribe3,
			String keyDescribe4, String keyDescribe5, String keyDescribe6, String keyDescribe7, String keyDescribe8) {
		Popularize entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "popularizeId", popularizeId);
		entity.setKeyDescribe1(keyDescribe1);
		entity.setKeyDescribe2(keyDescribe2);
		entity.setKeyDescribe3(keyDescribe3);
		entity.setKeyDescribe4(keyDescribe4);
		entity.setKeyDescribe5(keyDescribe5);
		entity.setKeyDescribe6(keyDescribe6);
		entity.setKeyDescribe7(keyDescribe7);
		entity.setKeyDescribe8(keyDescribe8);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
		// TODO Auto-generated method stub
		
	}
	@Override
	public Map<String,Object> queryPRByTitle(String title) {
		// TODO Auto-generated method stub
		Map<String,Object> map=new HashMap<String,Object>();
		List<?> ls1=extraSpringHibernateTemplate.getHibernateTemplate()
				.find("SELECT p,t.name FROM Popularize p ,Territory t WHERE  p.territoryId=t.territoryId AND p.title LIKE ?", "%"+title+"%" );
		List<?> ls2=extraSpringHibernateTemplate.getHibernateTemplate()
				.find("SELECT r,t.name  FROM ReleaseInformation r ,Territory t WHERE  r.territoryId=t.territoryId and r.status=1 and r.kWord LIKE ?", "%"+title+"%" );
		map.put("popularize", ls1);
		map.put("releaseInformation", ls2);
		return map;
	}
	@Override
	public void editById(Integer popularizeId, String content, String address, String title, String keyDescribe1,
			String keyDescribe2, String keyDescribe3, String keyDescribe4, String keyDescribe5, String keyDescribe6,
			String keyDescribe7, String keyDescribe8) {
		// TODO Auto-generated method stub
		Popularize entity=extraSpringHibernateTemplate.findFirstOneByPropEq(poc, "popularizeId", popularizeId);
		entity.setAddress(address);
		entity.setContent(content);
		entity.setTitle(title);
		entity.setKeyDescribe1(keyDescribe1);
		entity.setKeyDescribe2(keyDescribe2);
		entity.setKeyDescribe3(keyDescribe3);
		entity.setKeyDescribe4(keyDescribe4);
		entity.setKeyDescribe5(keyDescribe5);
		entity.setKeyDescribe6(keyDescribe6);
		entity.setKeyDescribe7(keyDescribe7);
		entity.setKeyDescribe8(keyDescribe8);
		extraSpringHibernateTemplate.getHibernateTemplate().update(entity);
	}
	
	
}
